﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Axium.Model;
using System.Configuration;

namespace Axium.DataAccess
{
    /// <summary>
    /// Concrete implementation of abstract repository. It knows what data sourse need to be queried
    /// </summary>
    public class DefaultAxiumRepository : IAxiumRepository
    {
        private readonly string _connectionString;

        public DefaultAxiumRepository()
        {
            _connectionString = ConfigurationManager.ConnectionStrings["Axium"].ConnectionString;
        }
        /// <summary>
        /// Here is sample of manual mapping of one data structure to other similar one. No automapper has been used
        /// GetAllModelsResult is auto-generated class. We do not want to expose it outside of our Data layer
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private static SomeModel FromSomeModelResult(GetAllModelsResult item)
        {
            if (item == null)
            {
                return null;
            }
            return new SomeModel
            {
                DateEntered = item.DateEntered,
                Discount = item.Discount,
                DrugName = item.DrugName,
                ICD9Primary = item.ICD9_Primary,
                FloatTest = item.FloatTest,
                HCPC = item.HCPC,
                HCPCBilling = item.HCPCBilling,
                Id = item.Id,
                MRN = item.MRN,
                OrderNum = item.OrderNum,
                QtyAllowed = item.QtyAllowed,
                NDC = item.NDC,
                Quantity = item.Quantity,
                Quote = item.Quote,
                Test = item.test,
                TotalQWP = item.TotalQWP,
            };
            
        }
        private static SomeModel FromSomeModelResult(GetModelResult item)
        {
            if (item == null)
            {
                return null;
            }
            return new SomeModel
            {
                DateEntered = item.DateEntered,
                Discount = item.Discount,
                DrugName = item.DrugName,
                ICD9Primary = item.ICD9_Primary,
                FloatTest = item.FloatTest,
                HCPC = item.HCPC,
                HCPCBilling = item.HCPCBilling,
                Id = item.Id,
                MRN = item.MRN,
                OrderNum = item.OrderNum,
                QtyAllowed = item.QtyAllowed,
                NDC = item.NDC,
                Quantity = item.Quantity,
                Quote = item.Quote,
                Test = item.test,
                TotalQWP = item.TotalQWP,
            };

        }

        public SomeModel GetItem(int id)
        {
            using (var dataContext = new AxiumDataContext(_connectionString))
            {
                var item = dataContext.GetModel(id).FirstOrDefault();
                return FromSomeModelResult(item);
            }
        }

        public IEnumerable<SomeModel> GetItems(string filter)
        {
            using (var dataContext = new AxiumDataContext(_connectionString))
            {
                return dataContext.GetAllModels().Where(x => string.IsNullOrWhiteSpace(filter) || x.DrugName.Contains(filter))
                    .Select(FromSomeModelResult).ToArray();
            }

        }
    }
}
